package com.example.javaproject;

import com.example.javaproject.okhttp.TextUtils;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.stream.JsonReader;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;

import okio.BufferedSource;
import okio.Okio;

public class JsonTest {
    public static void main(String[] args) throws InterruptedException {

        String json = "{\"code\":30052,\"language\":\"en\",\"clientOs\":\"aphone\",\"osVersion\":\"7.1.2\",\"appKey\":\"Perfect365_Explore_H5\",\"clientVersion\":\"8.27.3\",\"country\":\"us\",\"imei\":\"8ba6f011fdeb539e386d97c1f9b0902f\",\"model\":\"Nexus 5\",\"channel\":\"googlePlay\",\"userTags\":\"\"}";
        long t=System.currentTimeMillis();
        System.out.println(get(json));
        System.out.println((System.currentTimeMillis() - t));

        System.out.println(Math.ceil(1242f / 1080));
    }


    private static String get(String json) {
        int apiCode = 0;
        String language = null;
        lab:
        try (JsonReader jsonReader = new JsonReader(new StringReader(json))) {
            jsonReader.setLenient(true);
            String name = null;
            while (jsonReader.hasNext()) {
                switch (jsonReader.peek()) {
                    case NAME:
                        name = jsonReader.nextName();
                        break;
                    case NUMBER:
                        int value = jsonReader.nextInt();
                        if ("code".equals(name)) apiCode = value;
//                        if (apiCode != 0 && !TextUtils.isEmpty(language)) break lab;
                        break;
                    case STRING:
                        final String s = jsonReader.nextString();
                        if ("language".equals(name)) language = s;
//                        if (apiCode != 0 && !TextUtils.isEmpty(language)) break lab;
                        break;
                    case BEGIN_OBJECT:
                        jsonReader.beginObject();
                        break;
                    case END_OBJECT:
                        jsonReader.endObject();
                        break;
                    default:
                        jsonReader.skipValue();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return apiCode + language;
    }

    public static String readFile2String(String filePath) {
        if (null == (filePath)) {
            return null;
        }
        File file = new File(filePath);
        if (!file.exists()) return null;
        BufferedSource bufferedSource = null;
        try {
            InputStream steam = new FileInputStream(file);
            bufferedSource = Okio.buffer(Okio.source(steam));
            return bufferedSource.readByteString().utf8();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedSource != null) {
                try {
                    bufferedSource.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    private static void jacksonParser(String content) {
        JsonFactory jsonFactory = new JsonFactory();
        try {
            JsonParser parser = jsonFactory.createParser(new InputStreamReader(new ByteArrayInputStream(content.getBytes())));
            while (parser.nextToken() != com.fasterxml.jackson.core.JsonToken.END_OBJECT) {
                String key = parser.getCurrentName();
//                parser.nextToken();
                String value = parser.getText();
                System.out.println(key + ":" + value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void gsonParase(Gson gson, String content) {
        try {
            BaseMessageFormat baseMessageFormat = gson.fromJson(content, BaseMessageFormat.class);
            System.out.println(baseMessageFormat.data);
            Inner inner = gson.fromJson(baseMessageFormat.data, Inner.class);
            System.out.println(inner);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static final class Inner {
        public String a;
        public String b;

        @Override
        public String toString() {
            return "Inner{" +
                    "a='" + a + '\'' +
                    ", b='" + b + '\'' +
                    '}';
        }
    }


    private static final class BaseMessageFormat {
        public int code;
        public String msg;
        public JsonElement data;
    }
}
